package com.bidevalution.controller.api;

import com.bidevalution.business.service.BeSectionExpertInfoService;
import com.bidevalution.business.service.BeSectionStepService;
import com.bidevalution.business.service.BeSectionWarnMsgService;
import com.bidevalution.controller.util.RequestUtil;
import com.bidevalution.controller.vo.BeSectionLayoutVO;
import com.bidevalution.dao.entity.BeSectionExpertInfo;
import com.bidevalution.dao.entity.BeSectionStep;
import com.bidevalution.dao.entity.BeSectionWarnMsg;
import com.qtp.common.core.TSUser;
import com.qtp.common.data.RedisAccess;
import com.qtp.core.model.Response;
import com.qtp.core.util.GsonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 专家消息提醒控制层
 * Created by glodon on 2020-02-18.
 */
@Log4j2
@RestController
@RequestMapping("/beSectionWarnMsg")
@Api(tags = "专家消息提醒Controller")
public class BeSectionWarnMsgController {

    @Autowired
    private BeSectionWarnMsgService beSectionWarnMsgService;

    @Autowired
    private BeSectionExpertInfoService beSectionExpertInfoService;

    @Autowired
    private BeSectionStepService beSectionStepService;

    /**
     * 查询消息提醒信息列表
     * @param sectionStepId 标段环节ID
     * @return
     */
    @GetMapping("/getBeSectionWarMsgList")
    @ApiOperation(value = "查询消息提醒信息列表", notes = "查询消息提醒信息列表", response = BeSectionWarnMsg.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sectionStepId", value = "标段环节ID", required = true, dataType = "String", paramType = "query")
    })
    public Response getBeSectionWarMsgList(@RequestParam("sectionStepId") String sectionStepId) {
        try {
            String userGuid = RequestUtil.getHeader("userGuid");
            TSUser tsUser = RedisAccess.getValue(TSUser.class, userGuid);
            if (tsUser == null) {
                return Response.fail("获取专家账号登录信息失败");
            }
            BeSectionStep beSectionStep = beSectionStepService.getSectionStepBySeqId(sectionStepId);
            if (beSectionStep == null) {
                throw new Exception("未查询到该标段环节信息");
            }
            BeSectionExpertInfo beSectionExpertInfo = beSectionExpertInfoService
                    .selectByLoginName(beSectionStep.getSectionId(), tsUser.getLoginName());
            if (beSectionExpertInfo == null) {
                throw new Exception("当前用户不是该标段专家");
            }
            return Response.ok(beSectionWarnMsgService.getUntreatedWarnMsgList(sectionStepId, beSectionExpertInfo.getSeqId()));
        } catch (Exception e) {
            log.error("LOG[]getBeSectionWarMsgList_70[]{},error={}","异常日志",GsonUtil.toJsonWithGson(e));
            return Response.fail("查询消息提醒信息列表失败！");
        }
    }

    /**
     * 处理消息提醒信息
     * @param seqId 标段环节ID
     * @return
     */
    @PostMapping("/handleWarnMsg")
    @ApiOperation(value = "处理消息提醒信息", notes = "处理消息提醒信息", response = BeSectionLayoutVO.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "seqId", value = "消息提醒主键", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "remark", value = "回执说明", required = false, dataType = "String", paramType = "query")
    })
    public Response handleWarnMsg(@RequestParam("seqId") String seqId,
                                  @RequestParam(value="remark",required=false) String remark) {
        try {
            String userGuid = RequestUtil.getHeader("userGuid");
            TSUser tsUser = RedisAccess.getValue(TSUser.class, userGuid);
            if (tsUser == null) {
                return Response.fail("获取专家账号登录信息失败");
            }

            BeSectionWarnMsg beSectionWarnMsg = beSectionWarnMsgService.getBeSectionWarnMsg(seqId);
            if (beSectionWarnMsg == null) {
                throw new Exception("未查询到该消息提醒信息");
            }
            BeSectionExpertInfo beSectionExpertInfo = beSectionExpertInfoService
                    .selectByLoginName(beSectionWarnMsg.getSectionId(), tsUser.getLoginName());
            String expId = beSectionExpertInfo.getSeqId();
            if (!beSectionWarnMsg.getExpertId().equals(expId)) {
                throw new Exception("当前信息不属于该专家");
            }
            beSectionWarnMsgService.handleWarnMsg(seqId, expId,remark);
            return Response.ok();
        } catch (Exception e) {
            log.error("LOG[]handleWarnMsg_107[]{},error={}","异常日志", GsonUtil.toJsonWithGson(e));
            return Response.fail("处理消息提醒信息！");
        }
    }
}
